package com.demoo.zjsu.filter;


import com.demoo.zjsu.model.userinfo.UserInfo;
import com.demoo.zjsu.string.userinfo.Wechat;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ResourceBundle;

/**
 * 用户授权登录，获得id等等属性
 */
@Service
public class UserInfoInterceptor implements HandlerInterceptor {
    public boolean preHandle(javax.servlet.http.HttpServletRequest httpServletRequest,
                             javax.servlet.http.HttpServletResponse httpServletResponse, Object o)
            throws Exception {
            String responseURL = httpServletRequest.getRequestURL().toString();
            HttpSession session = httpServletRequest.getSession();
            UserInfo userInfo = (UserInfo) session.getAttribute("user");
            String code = httpServletRequest.getParameter("code");
            if (userInfo == null && code == null) {
                String scope = httpServletRequest.getParameter(Wechat.SCOPE);
                if (scope == null) {
                    scope = Wechat.SNSAPI_USERINFO;
                } else {
                    scope = Wechat.SNSAPI_BASE;
                }
                String getCode = Wechat.GETCODE
                        .replace(Wechat.SCOPE, scope)
                        .replace(Wechat.STATE, responseURL)
                        .replace(Wechat.REDIRECT_URI, responseURL)
                        .replace(Wechat.APPID, Wechat.appId);
                httpServletResponse.sendRedirect(getCode);
                return false;
            }
        return true;
    }

    public void postHandle(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    public void afterCompletion(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
